<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\api\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use QL\QueryList;
use GuzzleHttp\Exception\RequestException;
use app\core\log\Log4dd;

class GetGirls extends Command
{
    //php /home/web/project/admin.zhixianggo.com/think GetGirls
    protected function configure()
    {
        $this->setName('GetGirls')->setDescription('GetGirls');
    }

    protected function execute(Input $input, Output $output)
    {
        $headers[] = "DNT: 1";
        $headers[] = "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36";
        try {

            libxml_use_internal_errors(true);
            for ($i = 1; $i < 2; $i++) {
                $url = 'https://www.gravuregirlz.com/?vp_page=' . $i;
                $rules = [
                    'title' => ['#content .vp-portfolio__item-img a img', 'alt'],
                    'link' => ['#content .vp-portfolio__item-img a', 'href'],

                ];

                $rt = QueryList::get($url)->rules($rules)->query()->getData();

                $items = $rt->all();

                if ($items) {
                    foreach ($items as $item) {
                        if (isset($item['link'])) {
                            $url = $item['link'];
                            $rules = [
                                'page' => ['.wp-pagenavi .page', 'text'],
                            ];

                            $rt = QueryList::get($url)->rules($rules)->query()->getData();

                            $pages = $rt->all();

                            if ($pages) {
                                $total_page = $pages[(count($pages) - 3)]['page'];
                                for ($j = 1; $j <= $total_page; $j++) {
                                    $url = $item['link'] . $j . '/';
                                    $rules = [
                                        'url' => ['#gallery-1 .gallery-icon img', 'src'],
                                        //'image_name' => ['#gallery-1 .gallery-icon img', 'title'],
                                    ];

                                    $rt = QueryList::get($url)->rules($rules)->query()->getData();

                                    $imgs = $rt->all();
                                    $headers[] = 'Referer: ' . $url;
                                    if ($imgs) {
                                        foreach ($imgs as $img) {
                                            Log4dd::info('img => ' . $img);
                                            $res = downFile($img['url'], '/home/web/QueryList/' . $item['title'], $headers);
                                        }
                                    }
                                }
                            }
                        }
                        usleep(10000);
                    }
                }

                usleep(10000);

            }
        } catch (RequestException $e) {
            Log4dd::error('发生错误啦' . json_encode($e, JSON_UNESCAPED_UNICODE));
        }


    }

}
